﻿Imports Microsoft.VisualBasic

Public Enum EControlMode
    Editable = 0
    Disabled = 1
    Locked = 2
End Enum

Public MustInherit Class CCustomControl : Inherits UserControl

#Region "Members"
    Private m_requiredFormatString As String
#End Region

#Region "Event Handlers"
    Protected Overrides Sub OnInit(ByVal e As System.EventArgs)
        MyBase.OnInit(e)
        RestoreMode()
    End Sub
    Protected Overrides Sub OnPreRender(ByVal e As System.EventArgs)
        MyBase.OnPreRender(e)

        CtrlMain.Visible = Not Locked
        CtrlValidator.Visible = Not Locked
        If Locked Then CtrlLocked.Text = GetLockedText()
        If Locked OrElse Disabled Then Required = False
        CtrlLocked.Visible = Locked AndAlso CtrlLocked.Text.Length > 0
        CtrlMain.Enabled = Enabled
        CtrlLabel.Visible = Layout <> ELayout.None
        If Not IsNothing(CtrlDescription) AndAlso CtrlDescription.Text.Length = 0 Then CtrlDescription.Visible = False
    End Sub
#End Region

#Region "Appearance"
    Public Property Layout() As ELayout
        Get
            Return CtrlContainerBegin.Layout
        End Get
        Set(ByVal value As ELayout)
            CtrlContainerBegin.Layout = value
            CtrlContainerEnd.Layout = value
            CtrlSeparator1.Layout = value
            CtrlSeparator2.Layout = value
        End Set
    End Property
    Public Property Label() As String
        Get
            Return CtrlLabel.Text
        End Get
        Set(ByVal value As String)
            CtrlLabel.Text = value
            CtrlValidator.ErrorMessage = String.Format(RequiredFormatString, value)
        End Set
    End Property
    Public Property ShowLabel() As Boolean
        Get
            Return CtrlLabel.Visible
        End Get
        Set(ByVal value As Boolean)
            CtrlLabel.Visible = value
        End Set
    End Property
    Public Property Tooltip() As String
        Get
            Return CtrlMain.ToolTip
        End Get
        Set(ByVal value As String)
            CtrlMain.ToolTip = value
        End Set
    End Property
    Public Overridable Property RequiredFormatString() As String
        Get
            If String.IsNullOrEmpty(m_requiredFormatString) Then Me.RequiredFormatString = DefaultFormatRequiredText
            Return m_requiredFormatString
        End Get
        Set(ByVal value As String)
            m_requiredFormatString = value
            CtrlValidator.ErrorMessage = String.Format(value, Label)
        End Set
    End Property
#End Region

#Region "Behaviour"
    Public Property Mode() As EControlMode
        Get
            Return CType(CTextbox.GetInteger(CtrlHidden.Value), EControlMode)
        End Get
        Set(ByVal value As EControlMode)
            CtrlHidden.Value = CInt(value).ToString
        End Set
    End Property
    Public Property Required() As Boolean
        Get
            Return CtrlValidator.Enabled
        End Get
        Set(ByVal value As Boolean)
            CtrlValidator.Enabled = value
            If Not IsNothing(CtrlValidatorScript) Then CtrlValidatorScript.Visible = value
        End Set
    End Property

    'Derived
    Public ReadOnly Property Disabled() As Boolean
        Get
            Return Mode = EControlMode.Disabled
        End Get
    End Property
    Public ReadOnly Property Enabled() As Boolean
        Get
            Return Not Disabled
        End Get
    End Property
    Public ReadOnly Property Locked() As Boolean
        Get
            Return Mode = EControlMode.Locked
        End Get
    End Property
#End Region

#Region "Abstract/Virtual"
    'Presentation Logic
    Protected MustOverride Function GetLockedText() As String
    Protected MustOverride ReadOnly Property DefaultFormatRequiredText() As String

    'Active Controls
    Protected MustOverride ReadOnly Property CtrlLabel() As Literal
    Protected MustOverride ReadOnly Property CtrlLocked() As Label
    Protected MustOverride ReadOnly Property CtrlMain() As WebControl
    Protected MustOverride ReadOnly Property CtrlValidator() As BaseValidator
    Protected MustOverride ReadOnly Property CtrlHidden() As HiddenField
    Protected Overridable ReadOnly Property CtrlValidatorScript() As PlaceHolder
        Get
            Return Nothing 'Optional
        End Get
    End Property
    Protected Overridable ReadOnly Property CtrlDescription() As Label
        Get
            Return Nothing 'Optional
        End Get
    End Property

    'Layout Controls
    Protected MustOverride ReadOnly Property CtrlContainerBegin() As CCustomControlContainer
    Protected MustOverride ReadOnly Property CtrlContainerEnd() As CCustomControlContainer
    Protected MustOverride ReadOnly Property CtrlSeparator1() As CCustomControlContainer
    Protected MustOverride ReadOnly Property CtrlSeparator2() As CCustomControlContainer
#End Region

#Region "Private"
    Private Sub RestoreMode() 'in lieu of viewstate
        If Not Page.IsPostBack Then Exit Sub
        Dim key As String = CtrlHidden.UniqueID
        Dim value As String = Request.Form(key)
        Dim mode As Integer
        If Integer.TryParse(value, mode) Then Me.Mode = CType(mode, EControlMode)
    End Sub
#End Region

#Region "Shared"
    'Editable/Disabled/Locked
    Public Shared Sub SetControlMode(ByVal parent As Control, ByVal mode As EControlMode)
        If TypeOf parent Is CCustomControl Then CType(parent, CCustomControl).Mode = mode
        For Each i As Control In parent.Controls
            SetControlMode(i, mode)
        Next
    End Sub
#End Region

End Class
